import { eventFormat, stateFormat, bindEvent } from ".";

// 挂载DOM、渲染
export function useDOM({ state, template, methods }, DOM) {
  DOM.innerHTML = render(template, state);
  bindEvent(methods);
}

function render(template, state) { // 渲染模板
  template = eventFormat(template); // 专门处理绑定事件函数
  template = stateFormat(template, state); // 专门处理数据与模板
  // console.log(template);
  return template;
}

export function uodate(statePool, key, value) {
  // console.log(statePool, key, value);
  const allElements = document.getElementsByTagName('*');
  let oItem = null;

  statePool.forEach(item => {
    if (item.state[item.state.length - 1] === key) {
      for (let i = 0; i < allElements.length; i ++) {
        oItem = allElements[i];

        const _flag = parseInt(oItem.dataset.dom);

        if (item.flag === _flag) {
          console.log(item)
          oItem.innerHTML = value;
        }
      }
    }
  })
}
